import request from "@/utils/request.js"
const state = {
	data:[],//总数据
	hotRecommend:["茫茫甘露冰棒","火腿蛋可颂脆堡","夹心小方","生打耶耶奶冻","多肉葡萄","多肉葡萄","椰椰冻雪糕杯"],//热门推荐
	historyData:[],//搜索历史记录信息
	searchData:[],// 存放搜索数据
	detailData:null,//商品详情数据
}
const getters = {

}
const mutations = {
	// 商品总数据
	assignmentData(state,res){
		state.data = JSON.parse(JSON.stringify(res))
		// 每次调用这个方法的时候让searchData = []
		state.searchData = []
	},
	// 历史记录总信息
	historyData(state,res){
		state.historyData = JSON.parse(JSON.stringify(res))
	},
	// 搜索商品
	searchShopData(state,res){
		state.searchData = state.data.filter(item => {
			if(typeof res !== 'object'){
				return item.name.indexOf(res) !== -1
			}else{
				return null
			}
		})
	},
	// 筛选商品详情数据
	filterDetailData(state,res){
		console.log(res)
		state.detailData = null
		if(typeof res === 'object'){
			state.detailData = state.data.find(item => {
				return item.name.indexOf(res.shopName) !== -1
			})
		}else{
			state.detailData = state.data.find(item => {
			})
		}
	}
}
const actions = {
	// 获取商品总数据
	async getData(context){
		await request('/wx_data',{},"GET").then(res => {
			context.commit('assignmentData',res)
		})
	},
	// 获取历史记录总信息
	async gethistory(context){
		await request('/getsearchhistory',{},"GET").then(res => {
			context.commit('historyData',res)
		})
	},
	// 添加历史记录
	async addHistory(context,res){
		await request('/addHistory',{res},"GET").then(res => {
			context.commit('historyData',res)
		})
	},
	// 清除历史记录
	async delHistoryAll(context){
		await request('/delHistoryAll',{},"GET").then(res => {
			context.commit('historyData',res)
		})
	}
}
export
default {
	namespaced: true,
	state,
	getters,
	mutations,
	actions
}
